package com.slofzx.nuoda.daqing.wechat.util;

import com.alibaba.fastjson.JSON;
import com.slofzx.nuoda.daqing.wechat.dto.WechatDataDTO;
import com.slofzx.nuoda.daqing.wechat.enums.AccountTypeEnum;
import com.slofzx.nuoda.daqing.wechat.feign.WechatClient;
import com.slofzx.nuoda.daqing.wechat.vo.WechatResultVO;
import feign.RequestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 即时通工具类
 * @author sunfuyong
 */
@Component
public class WechatUtil {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private final String SEND_URL = "http://dqmds.petrochina/uimservice/wechat/send";

    @Value("${daqing.wechat.os-code}")
    private String osCode;
    @Autowired
    RestTemplate rawRestTemplate;
    /**
     * 发送消息
     * @param dto
     * @return
     */
    public WechatResultVO sendMessage(WechatDataDTO dto) {
        if(logger.isDebugEnabled()) {
            logger.debug("发送即时通消息:{}", JSON.toJSONString(dto));
        }
        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept-Encoding", "identity"); // 设置不接受压缩的内容

        HttpEntity<WechatDataDTO> entity = new HttpEntity<>(dto, headers);

        ResponseEntity<WechatResultVO> response = rawRestTemplate.exchange(
                SEND_URL,
                HttpMethod.POST,
                entity,
                WechatResultVO.class
        );
        WechatResultVO resultVO = response.getBody();
        if(logger.isDebugEnabled()) {
            logger.debug("发送即时通消息结果:{}", JSON.toJSONString(resultVO));
        }
        return resultVO;
    }
    /**
     * 根据用户账号向指定用户立即发送消息
     * @param accountType 账号类型
     * @param account 账号信息
     * @param content 发送内容
     * @param bizType 业务类型
     * @param bizCode 业务编号
     * @return
     */
    public WechatResultVO sendMessage(AccountTypeEnum accountType,String account, String content, String bizType, String bizCode) {
        return sendMessage(accountType,account,content,bizType,bizCode,null);
    }
    /**
     * 根据用户账号向指定定时用户发送消息
     * @param accountType 账号类型
     * @param account 账号信息
     * @param content 发送内容
     * @param bizType 业务类型
     * @param bizCode 业务编号
     * @param targetSendTime 计划发送时间
     * @return
     */
    public WechatResultVO sendMessage(AccountTypeEnum accountType,String account, String content, String bizType, String bizCode,String targetSendTime) {
        WechatDataDTO dto = new WechatDataDTO();
        switch (accountType) {
            case USERID:
                dto.setUserid(account);
            break;
            case EMAIL:
                dto.setEmail(account);
            break;
            case MOBILEPHONE:
                dto.setMobilephone(account);
            break;
            case WECHAT:
                dto.setWechat(account);
            break;

        }
        dto.setSendContent(content);
        dto.setOsCode(osCode);
        dto.setBizCode(bizCode);
        dto.setBizType(bizType);
        dto.setToSendDt(targetSendTime);
        if(logger.isDebugEnabled()) {
            logger.info("发送即时通消息:{}", JSON.toJSONString(dto));
        }
        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept-Encoding", "identity"); // 设置不接受压缩的内容

        HttpEntity<WechatDataDTO> entity = new HttpEntity<>(dto, headers);

        ResponseEntity<WechatResultVO> response = rawRestTemplate.exchange(
                SEND_URL,
                HttpMethod.POST,
                entity,
                WechatResultVO.class
        );
        WechatResultVO resultVO = response.getBody();
        logger.info("发送即时通消息结果:{}", JSON.toJSONString(resultVO));
        return resultVO;
    }

}
